cmake_minimum_required(VERSION 3.0)

if(0)
#先指定编译器，在执行cmake指令的时候，增加一个输入，-DBUILD_PLATFORM指定平台，支持windows（默认为64位）、linux-x86、linux-arm
if(${BUILD_PLATFORM} MATCHES "linux-arm")
    message("-- Build under the linux arm platform")
    set(CMAKE_SYSTEM_NAME ARM)
    set(CMAKE_SYSTEM_PROCESSOR $ENV{ARCH})
    #指定编译器
    #set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
    #set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
else()
    message("-- Build under the x86 platform")
    set(CMAKE_CXX_COMPILER g++)
    set(CMAKE_C_COMPILER gcc)
endif()
else()
    message("-- Build under the x86 platform")
    set(CMAKE_CXX_COMPILER g++)
    set(CMAKE_C_COMPILER gcc)
endif()

#指定版本信息
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(-fPIC)

if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
    message("-- Build type is Debug!")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O1 -static-libgcc -static-libstdc++")
    #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O1")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -Wall -O1")
else()
    message("-- Build type is Release!")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O3 -static-libgcc -static-libstdc++")
    #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -Wall -O3")
endif()

project(utility)

add_subdirectory(src/common)
add_subdirectory(src/communication)
add_subdirectory(src/test)
add_subdirectory(src/watchdog)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

#install(DIRECTORY ${PROJECT_SOURCE_DIR}/configs/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/configs)
install(FILES ${PROJECT_SOURCE_DIR}/configs/watchdog_cfg.ini DESTINATION ${EXECUTABLE_OUTPUT_PATH}/configs)

if(EXISTS ${PROJECT_SOURCE_DIR}/shells)
    install(FILES ${PROJECT_SOURCE_DIR}/shells/test.sh DESTINATION ${EXECUTABLE_OUTPUT_PATH}
        PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
    install(FILES ${PROJECT_SOURCE_DIR}/shells/watchdog.sh DESTINATION ${EXECUTABLE_OUTPUT_PATH}
        PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endif()

if(0)
#暂时列从可支持编译平台
if(${BUILD_PLATFORM} MATCHES "linux-arm")
    #linux-arm平台
    install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/arm/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/)
else()
    #if(CMAKE_SYSTEM_NAME MATCHES "Windows")  #windows平台
    if(${BUILD_PLATFORM} MATCHES "windows")
        #windows平台
        #install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/win/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/win/mosquitto/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
    else()
        #linux-x86平台
        #install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/mosquitto/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
    endif()
endif()
else()
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")  #windows平台
    #if(${BUILD_PLATFORM} MATCHES "windows")
        #windows平台
        #install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/win/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/win/mosquitto/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/win/civetweb/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
    else()
        #linux-x86平台
        #install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/mosquitto/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
        install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/civetweb/ DESTINATION ${EXECUTABLE_OUTPUT_PATH}/lib)
    endif()
endif()

add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target clean
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_SOURCE_DIR}/build/
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/build/
    COMMENT "-- Cleaning the build directory..."
    VERBATIM)


